// 通用玩家相关接口
const mongoose = require('mongoose');
const Good = require('../models/Goods/Good');
const UserGood = require("../models/User/User_good");
const Equipment = require("../models/Equipments/Equipment");
const UserEquipment = require("../models/User/User_equipment");

// 引入tools中的方法
const equip_tools = require('../controller/tools/equipment/equip');

exports.addusergood = async (req, res) => {
    //console.log(req.user._id);
    //console.log(req.params.id);
    const newUserGood = {};
    if (req.user._id) { newUserGood.user = req.user._id };
    if (req.params.id) { newUserGood.goods = req.params.id };
    new UserGood(newUserGood).save()
        .then(good => res.json(good))
        .catch(err => console.log(err));
};

exports.getusergood = async (req, res) => {
    let uid = req.user._id
    if (!uid) {
        return res.json({ code: 0, msg: "玩家背包不存在", data: null })
    } else {
        usergoods = await UserGood.find({ user: uid })
        .populate('goods', 'name info')
        .exec((err, usergood) => {
            if (err) {
                return res.status(404).json(err);
            }
            //console.log(usergood);
            return res.json(usergood);
        });;
    }
};

exports.getuserequipment = async (req, res) => {
    let uid = req.user._id
    if (!uid) {
        return res.json({ code: 0, msg: "玩家不存在", data: null })
    } else {
        userequipment = await UserEquipment.find({ user: uid })
            .populate(
                {
                    path: 'equipment',
                    populate: { path: 'equipments_type task' }
                }
            )
            .populate('skill')
            .exec((err, userequipments) => {
                //console.log(userequipments);
                if (err) {
                    return res.status(404).json(err);
                }
                return res.json(userequipments);
            });;
    }
};
// 获取玩家武器背包
exports.getuserweaponbag = async (req, res) => {
    let uid = req.user._id
    if (!uid) {
        return res.json({ code: 0, msg: "玩家不存在", data: null })
    } else {
        userequipment = await UserEquipment.find({ user: uid })
            .populate(
                {
                    path: 'equipment',
                    populate: { path: 'equipments_type task' }
                }
            )
            .populate('skill')
            .exec((err, userequipments) => {
                //console.log(userequipments);
                for (let i = userequipments.length - 1; i >= 0; i--) {
                    // if(userequipments[i] == 2){
                    //     userequipments.splice(i,1);
                    // }
                    if (userequipments[i].equipment.equipments_type.name != '武器') {
                        userequipments.splice(i, 1);
                    };
                };
                if (err) {
                    return res.status(404).json(err);
                }
                return res.json(userequipments);
            });;
    }
};
// 获取玩家衣服背包
exports.getuserclothesbag = async (req, res) => {
    let uid = req.user._id
    if (!uid) {
        return res.json({ code: 0, msg: "玩家不存在", data: null })
    } else {
        userequipment = await UserEquipment.find({ user: uid })
            .populate(
                {
                    path: 'equipment',
                    populate: { path: 'equipments_type task' }
                }
            )
            .populate('skill')
            .exec((err, userequipments) => {
                //console.log(userequipments);
                for (let i = userequipments.length - 1; i >= 0; i--) {
                    // if(userequipments[i] == 2){
                    //     userequipments.splice(i,1);
                    // }
                    if (userequipments[i].equipment.equipments_type.name != '衣服') {
                        userequipments.splice(i, 1);
                    };
                };
                if (err) {
                    return res.status(404).json(err);
                }
                return res.json(userequipments);
            });;
    }
};
// 获取玩家鞋子背包
exports.getusershoebag = async (req, res) => {
    let uid = req.user._id
    if (!uid) {
        return res.json({ code: 0, msg: "玩家不存在", data: null })
    } else {
        userequipment = await UserEquipment.find({ user: uid })
            .populate(
                {
                    path: 'equipment',
                    populate: { path: 'equipments_type task' }
                }
            )
            .populate('skill')
            .exec((err, userequipments) => {
                for (let i = userequipments.length - 1; i >= 0; i--) {
                    // if(userequipments[i] == 2){
                    //     userequipments.splice(i,1);
                    // }
                    if (userequipments[i].equipment.equipments_type.name != '鞋子') {
                        userequipments.splice(i, 1);
                    };
                };
                if (err) {
                    return res.status(404).json(err);
                }
                return res.json(userequipments);
            });;
    }
};
// 获取玩家戒指背包
exports.getuserringbag = async (req, res) => {
    let uid = req.user._id
    if (!uid) {
        return res.json({ code: 0, msg: "玩家不存在", data: null })
    } else {
        userequipment = await UserEquipment.find({ user: uid })
            .populate(
                {
                    path: 'equipment',
                    populate: { path: 'equipments_type task' }
                }
            )
            .populate('skill')
            .exec((err, userequipments) => {
                for (let i = userequipments.length - 1; i >= 0; i--) {
                    // if(userequipments[i] == 2){
                    //     userequipments.splice(i,1);
                    // }
                    if (userequipments[i].equipment.equipments_type.name != '戒指') {
                        userequipments.splice(i, 1);
                    };
                };
                if (err) {
                    return res.status(404).json(err);
                }
                return res.json(userequipments);
            });
    }
};
// 获取玩家穿戴中背包
exports.getuserusingbag = async (req, res) => {
    let uid = req.user._id
    if (!uid) {
        return res.json({ code: 0, msg: "玩家不存在", data: null })
    } else {
        userequipment = await UserEquipment.find({ user: uid })
            .populate(
                {
                    path: 'equipment',
                    populate: { path: 'equipments_type task' }
                }
            )
            .populate('skill')
            .exec((err, userequipments) => {
                for (let i = userequipments.length - 1; i >= 0; i--) {
                    // if(userequipments[i] == 2){
                    //     userequipments.splice(i,1);
                    // }
                    if (userequipments[i].status != 1) {
                        userequipments.splice(i, 1);
                    };
                };
                if (err) {
                    return res.status(404).json(err);
                }
                return res.json(userequipments);
            });
    }
};

// 穿戴装备
exports.userequip = async (req, res) => {
    let uid = req.user._id;
    let eid = req.params.id;
    if (!uid || !eid) {
        return res.status(404).json({ code: 0, msg: "参数有误", data: null })
    }
    if (await equip_tools.check_in_staus(uid, eid)) {
        // 判断一下玩家是不是还拥有这个装备
        const userequipment = await UserEquipment.find({ _id: eid })
        if (userequipment.length > 0) {
            const User_Equipment_Fields = { $set: { status: 1 } };
            await UserEquipment
                .updateOne({ _id: eid }, User_Equipment_Fields, { new: true })
                .then(equipment => res.json({ code: 200, msg: "装备成功" }))
        }
    } else {
        return res.json({ code: 1, msg: "玩家该部位已佩戴装备", data: null })
    }
}
// 卸下装备
exports.userequipoff = async (req, res) => {
    let uid = req.user._id;
    let eid = req.params.id;
    if (!uid || !eid) {
        return res.status(404).json({ code: 0, msg: "参数有误", data: null })
    }
    const userequipment = await UserEquipment.find({ _id: eid })
    if (userequipment.length > 0) {
        const User_Equipment_Fields = { $set: { status: 0 } };
        await UserEquipment
            .updateOne({ _id: eid }, User_Equipment_Fields, { new: true })
            .then(equipment => {
                res.json({ code: 200, msg: "卸下成功" });
            })
    }
}